package com.tutos.ui.student;

import java.util.Arrays;
import java.util.List;

import com.google.inject.Inject;
import com.tutos.GuiceServletInjector;
import com.tutos.dao.MonitorDAO;
import com.tutos.dao.TutoriaDAO;
import com.tutos.model.Monitor;
import com.tutos.model.Tutoria;
import com.tutos.ui.MenuComponent;
import com.tutos.ui.TutosApplication;
import com.tutos.ui.monitor.Calificacion;
import com.tutos.ui.monitor.HorarioGrafico;
import com.tutos.ui.monitor.MonitorData;
import com.tutos.ui.monitor.Tutorial;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class SearchTutoria implements MenuComponent{

	enum Filtro{
		MONITOR, MATERIA
	}
	
	private static final List<String> values = Arrays.asList(new String[]{"Monitor", "Materia"});
	
	private VerticalLayout mainPanel = new VerticalLayout();
	
	private final TutoriaDAO tutoriaDAO;
	private final MonitorDAO monitorDAO;

	
	private TextField textField = new TextField();
	
	private Button buscar = new Button("Buscar");
	
	private OptionGroup optionGroup  = new OptionGroup("Seleccion el criterio de busqueda:", values);

	
	VerticalLayout results = new VerticalLayout();
	
	@Inject
	public SearchTutoria(TutoriaDAO tutoriaDAO, MonitorDAO monitorDAO) {
		this.tutoriaDAO = tutoriaDAO;
		this.monitorDAO = monitorDAO;

		buildLayout();
	}
	
	
	private void buildLayout(){
		
		buscar.addListener(new ClickListener() {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				switch (getTypeSearch()) {
				case MONITOR:
					showResults(tutoriaDAO.getTutoriasMonitorNombre(getSearchCriteria()));
					break;
				case MATERIA:
					showResults(tutoriaDAO.getTutoriasMateriaNombre(getSearchCriteria()));
					break;
				}
			}
		});
		
		HorizontalLayout mainHorizontalLayout = new HorizontalLayout();
		mainHorizontalLayout.setWidth("60%");
		
		HorizontalLayout searchLayout = new HorizontalLayout();
		
		searchLayout.addComponent(textField);
		
		searchLayout.addComponent(buscar);
		
		mainHorizontalLayout.addComponent(searchLayout);
		mainHorizontalLayout.setComponentAlignment(searchLayout, Alignment.TOP_LEFT);
		
		optionGroup.select(values.get(0));
		
		mainHorizontalLayout.addComponent(optionGroup);
		mainHorizontalLayout.setComponentAlignment(optionGroup, Alignment.TOP_RIGHT);
		
		
		mainPanel.addComponent(mainHorizontalLayout);
		mainPanel.setComponentAlignment(mainHorizontalLayout, Alignment.TOP_CENTER);
		
		
		mainPanel.addComponent(results);
		results.setWidth("100%");
		
	}

	
	private void showResults(List<Tutoria> tutorias){
		
		results.removeAllComponents();
		
		for(Tutoria tutoria : tutorias){
			Monitor monitor = monitorDAO.getEntity(tutoria.getMonitor().getId());
			
			results.addComponent(new ResultItem(tutoria, monitor));
		}
		
	}
	
	private Filtro getTypeSearch(){
		if(optionGroup.getValue().equals(values.get(0))){
			return Filtro.MONITOR;
		}
		
		return Filtro.MATERIA;
	}
	
	private String getSearchCriteria(){
		return (String) textField.getValue();
	}


	@Override
	public Component getComponent() {
		return mainPanel;
	}
	
	private class ResultItem extends CustomComponent {
		private String xhtml = "<i	>This</i > is an <b >XHTML</b > formatted label";
		private static final long serialVersionUID = 1L;
		
		private Label description = new Label(xhtml,Label.CONTENT_XHTML);
		private Button buttonTutoria = new Button();
		
		private Monitor monitor;
		
		public ResultItem(Tutoria tutoria, Monitor monitor) {
			super(new VerticalLayout());
			buildLayout();
			buttonTutoria.setIcon(new ThemeResource("perfil.gif"));
			buttonTutoria.setDescription("Ver perfil");
			this.monitor = monitor;
			
			description.setValue("<b>"+tutoria.getInformacion()+"<b>");
		}
	

			
			
		
		private VerticalLayout buildLayout(){
			setWidth("100%");
			setHeight("160px");
			
			VerticalLayout layout = (VerticalLayout) getCompositionRoot();
			layout.setStyleName("result-entry");
			layout.setWidth("100%");
			layout.setHeight("140px");
			layout.setMargin(true);
			
			layout.addComponent(description);
			
			HorizontalLayout horizontalLayout = new HorizontalLayout();
			
			buttonTutoria.addListener(new ClickListener() {
				
				private static final long serialVersionUID = 1L;
				
				@Override
				public void buttonClick(ClickEvent event) {
					TutoriaView view = new TutoriaView(monitor);
					
					Window window = new Window();
					window.getContent().setSizeUndefined();
					window.setSizeUndefined();
					window.setModal(true);
					
					window.addComponent(view.getComponent());
					
					TutosApplication.getInstance().getMainWindow().addWindow(window);
				}
			});
			
			horizontalLayout.addComponent(buttonTutoria);
			
			layout.addComponent(horizontalLayout);
			layout.setComponentAlignment(horizontalLayout, Alignment.BOTTOM_RIGHT);
			
			return layout; 
		}	
		
		
	}
	
	private class TutoriaView implements MenuComponent {
		
		VerticalLayout layout = new VerticalLayout();
		HorarioGrafico  horarioGrafico = GuiceServletInjector.injector.getInstance(HorarioGrafico.class);
		Tutorial  tutorial = GuiceServletInjector.injector.getInstance(Tutorial.class);

    	Calificacion calificacion = GuiceServletInjector.injector.getInstance(Calificacion.class);
		MonitorData monitorData = new MonitorData(calificacion.getComponent());
		
		public TutoriaView(Monitor monitor) {
			TabSheet tabSheet = new TabSheet();
			tabSheet.setWidth("770px");
			tabSheet.setHeight("580px");
			
			horarioGrafico.init(monitor);
			horarioGrafico.getComponent().setHeight("550px");
			horarioGrafico.getComponent().setWidth("750px");
			tutorial.init(monitor);
			monitorData.init(monitor);
			calificacion.init(monitor);
			
			tabSheet.addTab(monitorData.getComponent(), "Datos Monitor");
			tabSheet.addTab(horarioGrafico.getComponent(), "Horario Monitor");
			tabSheet.addTab(tutorial.getComponent(), "Información Tutorias");

			
			layout.addComponent(tabSheet);
		}
		
		@Override
		public Component getComponent() {
			return layout;
		}
		
	}
	
	
}
